﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JsonToSql
{
    /// <summary>
    /// Some table with conditions
    /// </summary>
    public class JsonTableCondition
    {
        /// <summary>
        /// Name of table
        /// </summary>
        public string Name
        {
            get
            {
                if (token != null)
                {
                    return token.Path.Substring(2);
                }
                return null;
            }
        }
        private ReadOnlyCollection<JsonCondition> conditions;
        /// <summary>
        /// Conditions for deletion
        /// </summary>
        public ReadOnlyCollection<JsonCondition> Conditions
        {
            get
            {
                return conditions;
            }
        }
          private JToken token;
        private JsonRequest parent;
        /// <summary>
        /// Parent request
        /// </summary>
        public JsonRequest Parent
        {
            get
            {
                return parent;
            }
        }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="token">Token text format of table</param>
        /// <param name="parent">Parent request</param>
        public JsonTableCondition(JToken token, JsonRequest parent)
        {
            this.parent = parent;
            this.token = token;
            createConditions();
           
        }
        private void createConditions()
        {

            conditions=new ReadOnlyCollection<JsonCondition>((from item in token.Values().ToList()
             select new JsonCondition(item, this)).ToList());

        }
    }
}
